const role = {
	mill: '本厂',
	client: '客户',
	dev: '开发人员'
}

let openId="";
let nickName="";
let realName="";
let millName="";

// 初始化页面 - 执行逻辑
function initPage(){
	openId = $.request.queryString["openId"];
	nickName = $.request.queryString["nickName"];
	realName = $.request.queryString["realName"];
	millName = $.request.queryString["millName"];
	millIid = $.request.queryString["millIid"];

	// 根据 millIid 再次获取 mill
	const millUrl = getMillUrl(millIid);
	console.log(`请求mill的url:${millUrl}`)
	$.ajax({
		url : millUrl,
    type : "POST",
    contentType: "application/json;charset=UTF-8",
    success : function(res) {
    	if(res.code !== 1){
    		alert(res.msg)
    		return
    	}

    	millName = res.data.sid;
    	$(`#mill-name`).html(millName);
    },
    error : function(e){
      console.log(e.status);
      console.log(e.responseText);
    },
    complete: function () {
    }
  })

	$(`#open-id`).html(openId);
	$(`#nick-name`).html(nickName);
	$(`#real-name`).html(realName);
	$(`#mill-name`).html(millName);
}

// 初始化页面 - 启动方法
$(function(){
	initPage();
})


function setBtnDisabled(disabled){
	document.getElementById("btn1").disabled = disabled;
	document.getElementById("btn2").disabled = disabled;
	document.getElementById("btn3").disabled = disabled;
}

// 审核当前用户为工厂
function registerMill(){
	register(role.mill);
}

// 审核为收货人
function registerClient(){
	register(role.client);
}

// 审核为本司人员
function registerDev(){
	register(role.dev);
}

function register(role){
	setBtnDisabled(true);

	$(`#req-success`).html('');
	const url = getRegisterNewUserUrl();
	const code = $.request.queryString["code"];
	const data = {"openId":openId,"nickName":nickName,"realName":realName,"millName":millName,"code":code,"role":role};
	
	$.ajax({
		url : url,
    type : "POST",
    contentType: "application/json;charset=UTF-8",
    data:JSON.stringify(data),
    success : function(res) {
    	if(!res) return;
    	if(res.code === 1)
 				$(`#req-success`).html('新增用户成功，请退出本页面！');
 			else
 				$(`#req-success`).html(res.msg);
    },
    error : function(e){
      console.log(e.status);
      console.log(e.responseText);
    },
    complete: function () {
      setBtnDisabled(false);
    }
  })
}

$.request = (function () {
  var apiMap = {}; 
  function request(queryStr) { 
      var api = {}; 
      if (apiMap[queryStr]) { return apiMap[queryStr]; } 
      api.queryString = (function () { 
          var urlParams = {}; 
          var e, 
          d = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); }, 
          q = queryStr.substring(queryStr.indexOf('?') + 1), 
          r = /([^&=]+)=?([^&]*)/g; 
          while (e = r.exec(q))     urlParams[d(e[1])] = d(e[2]);
          return urlParams; 
      })(); 
      api.getUrl = function () { 
          var url = queryStr.substring(0, queryStr.indexOf('?') + 1); 
          for (var p in api.queryString) { url += p + '=' + api.queryString[p] + "&";        } 
          if (url.lastIndexOf('&') == url.length - 1) { return url.substring(0, url.lastIndexOf('&')); } 
          return url; 
      } 
      apiMap[queryStr] = api; 
      return api; 
  } 
  $.extend(request, request(window.location.href)); 
  return request; 
})();

/*************************** 下面是废弃的方法 *********************************/
// 给准备新注册的微信用户推送模板消息
// 提示重新扫码关注
function repush(){
	// const url = "../wxbebarrel/tmpmsg/w";
	const url = getReRegisterTemplateMsgUrl();
	const msgData = {
		"metaList":[
				{"value":"注册方式错误，请先取消关注，向开发商索要二维码进行扫描"},
				{"value":"使用微信扫描后重新填写真实姓名再提交"},
				{"value":JSON.stringify(new Date())},
				{"value":""},{"value":""},{"value":""},{"value":""},{"value":""}
			],
		"openidList":[openId],
		"templateId":RE_REGISTER_TEMPLATEMSG_ID
	}

	$.ajax({
		url : url,
    type : "POST",
    contentType: "application/json;charset=UTF-8",
    data:JSON.stringify(msgData),
    success : function(res) {
    	console.log('推送后的响应：',res);
    },
    error : function(e){
      console.log(e.status);
      console.log(e.responseText);
    },
    complete: function () {
    }
  })
}
